package com.sparrow.manage.comment;

import com.sparrow.config.EnvironmentVariables;
import com.sparrow.manage.common.controller.StandardController;
import com.sparrow.manage.common.entity.result.Result;
import com.sparrow.manage.common.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@Controller
@RequestMapping(EnvironmentVariables.MANAGE_ACCESS_PREFIX + "/comment")
public class CommentController extends StandardController<Comment> {

    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;

    @Autowired
    private HttpServletRequest request;

    @Override
    protected BaseService getService() {
        return this.commentService;
    }

    @Override
    protected Result argumentsValidate(Comment comment) {
        Result result = new Result();
        result.setSuccess(true);
        if(StringUtils.isBlank(comment.getVisitorName())){
            result.setMsg("姓名为空");
            result.setSuccess(false);
        }
        if(StringUtils.isBlank(comment.getVisitorLink())){
            result.setMsg("联系方式为空");
            result.setSuccess(false);
        }
        if(StringUtils.isBlank(comment.getContent())){
            result.setMsg("评论内容为空");
            result.setSuccess(false);
        }
        return result;
    }

    public Result sayHello(Comment comment){
        Result result = this.addEntity(comment);
        if(result.isSuccess()){
            this.commentService.deleteHistory();
        }
        return result;
    }

    @Override
    protected void pushCreateUser(Comment comment) {
        if(request != null && "/external/sayHello".equals(request.getRequestURI())){
            comment.setCreateUser("external");
            comment.setCreateTime(new Date());
        }else{
            super.pushCreateUser(comment);
        }
    }
}
